//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents the root configuration for the Resource Application Block.
	/// </summary>
	[Image(typeof(ResourceManagerSettingsNode))]
	[SelectedImage(typeof(ResourceManagerSettingsNode))]
	public class ResourceManagerSettingsNode : ConfigurationSectionNode
	{
		#region Fields
		private ResourceManagerNode resourceManagerNode;
		private EventHandler<ConfigurationNodeChangedEventArgs> resourceManagerNodeRemovedHandler;
		#endregion

		#region Properties
		/// <summary>
		/// <para>Gets the name for the node.</para>
		/// </summary>
		/// <value>
		/// <para>The display name for the node.</para>
		/// </value>		
		[ReadOnly(true)]
		public override string Name
		{
			get { return base.Name; }
		}

		/// <summary>
		/// Gets or sets the default Resource manager
		/// </summary>
		/// <value>
		/// The default Resource manager.
		/// </value>
		[Required]
		[Editor(typeof(ReferenceEditor), typeof(UITypeEditor))]
		[ReferenceType(typeof(ResourceManagerNode))]
		[ResourceCategory("CategoryGeneral")]
		[ResourceDescription("DefaultResourceManagerDescription")]
		public ResourceManagerNode DefaultResourceManager
		{
			get { return resourceManagerNode; }
			set
			{
				resourceManagerNode = LinkNodeHelper.CreateReference<ResourceManagerNode>(resourceManagerNode, value, resourceManagerNodeRemovedHandler, null);
			}
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceManagerSettingsNode"/> class.
		/// </summary>		
		public ResourceManagerSettingsNode ()
			: base(InternalResources.DefaultResourceManagerSettingsNodeName)
		{
			this.resourceManagerNodeRemovedHandler = new EventHandler<ConfigurationNodeChangedEventArgs>(OnResourceManagerNodeRemoved);
		}
		#endregion

		#region Protected Override Methods
		/// <summary>
		/// <para>Releases the unmanaged resources used by the <see cref="ResourceManagerSettingsNode "/> and optionally releases the managed resources.</para>
		/// </summary>
		/// <param name="disposing">
		/// <para><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</para>
		/// </param>
		protected override void Dispose (bool disposing)
		{
			try
			{
				if ( disposing )
				{
					if ( null != resourceManagerNode )
					{
						resourceManagerNode.Removed -= resourceManagerNodeRemovedHandler;
					}
				}
			}
			finally
			{
				base.Dispose(disposing);
			}
		}
		#endregion

		#region Protected Events
		/// <summary>
		/// Raises the <see cref="ConfigurationNode.ChildAdded"/> event and confirms that only one <see cref="ResourceManagerCollectionNode"/> has been added.
		/// </summary>
		/// <param name="e">A <see cref="ConfigurationChangedEventArgs"/> that contains the event data.</param>
		protected override void OnChildAdded (ConfigurationNodeChangedEventArgs e)
		{
			base.OnChildAdded(e);

			if ( Nodes.Count > 1 && e.Node.GetType() == typeof(ResourceManagerCollectionNode) )
			{
				throw new InvalidOperationException(InternalResources.ExceptionOnlyOneResourceManagerCollectionNode);
			}
		}
		#endregion

		#region Private Events
		private void OnResourceManagerNodeRemoved (object sender, ConfigurationNodeChangedEventArgs e)
		{
			this.resourceManagerNode = null;
		}
		#endregion
	}
}